package com.example.taximeter.config;

import com.example.taximeter.bean.User;
import com.example.taximeter.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 认证授权
 */
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    //认证.登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken utoken=(UsernamePasswordToken) token;//获取用户输入的token
        String username = utoken.getUsername();
        User user = userService.findUserByUsername(username);
        if(user==null){
            throw new UnknownAccountException();//没有找到账号异常
        }
//        if(user.getUserexp().before(GlobalParam.getNowDate())){
//            throw new LockedAccountException();//抛出账号锁定异常
//        }
        return new SimpleAuthenticationInfo(user, user.getPasswd(),this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal){
//        User user=(User) principal.fromRealm(this.getClass().getName()).iterator().next();//获取session中的用户
//        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//        info.addStringPermission(user.getRoleid());//将权限放入shiro中.
//        return info;
        return null;
    }
}
